{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T13:47:13.028996Z",
     "start_time": "2025-08-18T13:46:59.089017Z"
    }
   },
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "import os\n",
    "chatLLM = ChatOpenAI(\n",
    "    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "    api_key=os.getenv(\"DASH_SCOPE_API_KEY\"), # 如何获取API Key：https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key\n",
    "    base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    model=\"qwen-plus\",\n",
    "    temperature=0.8,\n",
    "    max_tokens=60,\n",
    ")\n",
    "\n",
    "template = \"\"\"您是一位专业的鲜花店文案撰写员。\\n\n",
    "对于售价为 {price} 元的 {flower_name} ，您能提供一个吸引人的简短描述吗？\"\"\"\n",
    "\n",
    "prompt = PromptTemplate.from_template(template)\n",
    "\n",
    "# 打印LangChain提示模板的内容\n",
    "print(prompt)\n",
    "\n",
    "# 多种花的列表\n",
    "flowers = [\"玫瑰\", \"百合\", \"康乃馨\"]\n",
    "prices = [\"50\", \"30\", \"20\"]\n",
    "\n",
    "for flower, price in zip(flowers, prices):\n",
    "    input = prompt.format(flower_name=flower, price=price)\n",
    "    output = chatLLM.invoke(input)\n",
    "    print(output.content)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input_variables=['flower_name', 'price'] input_types={} partial_variables={} template='您是一位专业的鲜花店文案撰写员。\\n\\n对于售价为 {price} 元的 {flower_name} ，您能提供一个吸引人的简短描述吗？'\n",
      "当然可以！以下是一段简洁又富有吸引力的玫瑰鲜花文案：\n",
      "\n",
      "**“50元，送她一束炙热的浪漫。经典红玫瑰，为爱绽放，每一瓣都是心动的形状。”**\n",
      "\n",
      "如果您有特定的花束风格、节日主题或目标人群，我也可以进一步优化文案。\n",
      "当然可以！以下是一个吸引人的简短鲜花文案：\n",
      "\n",
      "**“纯净如初见，百合绽放爱的低语。30元，让心意在芬芳中轻轻诉说。”**\n",
      "\n",
      "如果您需要适用于不同场景（如节日、表白、慰问等）的版本，我也可以继续优化调整。\n",
      "当然可以！以下是一个吸引人的康乃馨鲜花文案：\n",
      "\n",
      "**「爱在细节中绽放——20元尽享温馨心意」**\n",
      "\n",
      "柔美花瓣承载真挚情感，一支康乃馨，是对妈妈的感恩、对老师的敬意，或对爱人温柔心意的最佳表达。20元，把温暖送至心间。\n",
      "\n",
      "如需根据节日或场景定制文案，也可以告诉我，我来为您优化！\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "c0069655fa4ce773"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
